home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1734 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  53 lines

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer-to-Double as Function Arg
  5. Date: 16 Jan 1996 09:25:41 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4dgn2l$59p@crl.crl.com>
  8. References: <4dfccl$j5h@colossus.holonet.net>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. mitch@news.mdli.com (Mitch Miller) writes:
  12.  
  13. >I'm having a problem with a function that takes a
  14. >couple of pointer-to-double arguments, initializes then
  15. >and assigns values to them in an array style:
  16.  
  17. >    int GetValue( long iDim, double * Values1, double *
  18. >      Values2 )
  19. >    {
  20. >        int iter;
  21. >        Values1 = (double *) malloc( iDim * sizeof( double ));
  22. >        if (Values1 == NULL )
  23. >            ....
  24. >        Values2 = (double *) malloc( iDim * sizeof( double ));
  25. >        if (Values2 == NULL )
  26. >            ...
  27. >    }
  28.  
  29. This is another case of misunderstanding what call-by-value does. You are 
  30. passing pointers to two doubles to GetValue, and then trying to modify 
  31. those pointers (with the malloc() calls). You then go on to assign data 
  32. to what's pointed to correctly, but when you return, your modified values 
  33. are lost.
  34.  
  35. What you need to do is declare GetValue as:
  36.  
  37. int GetValue( int iDim, double **Values1, double **Values2)
  38. {
  39.     ...
  40.     *Values1 = malloc(...);
  41.     *Values2 = malloc(...);
  42.     ...
  43. }
  44. and then call it with:
  45. double *p1, *p2;
  46. int Size = whatever;
  47.  
  48. if ( GetValue( Size, &p1, &p2))
  49.     ...
  50.  
  51. I hope that helps.
  52.   Bob
  53.